package com.etang.a2020a1018.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.etang.a2020a1018.R;
import com.etang.a2020a1018.tools.AppConfig;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends Fragment {

    private TextView tv_gas, tv_press, tv_smo, tv_co, tv_pm, tv_per;
    private CheckBox cb_per;
    private ListView lv_link_state;
    private List<String> list = new ArrayList<String>();
    private SharedPreferences sharedPreferences;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, null, false);
        initView(view);
        initList();
        handler.post(time);
        return view;
    }

    private void initList() {
        list.clear();
        list.add("烟雾模式：" + sharedPreferences.getString("smo_state", null));
        list.add("光照模式：" + sharedPreferences.getString("ill_state", null));
        list.add("温度模式：" + sharedPreferences.getString("temp_state", null));
        list.add("绿色模式：" + sharedPreferences.getString("per_state", null));
        list.add("二氧化碳模式：" + sharedPreferences.getString("co_state", null));
        ListAdapter listAdapter = new ArrayAdapter<>(getActivity(), R.layout.simple_list_item_1, list);
        lv_link_state.setAdapter(listAdapter);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            tv_co.setText(String.valueOf(AppConfig.co));
            tv_gas.setText(String.valueOf(AppConfig.gas));
            tv_pm.setText(String.valueOf(AppConfig.pm));
            tv_press.setText(String.valueOf(AppConfig.press));
            tv_smo.setText(String.valueOf(AppConfig.smo));
            if (AppConfig.per == 0) {
                tv_per.setText("有人");
                cb_per.setChecked(true);
            } else {
                tv_per.setText("无人");
                cb_per.setChecked(false);
            }
            initList();
            if (sharedPreferences.getString("smo_state", null).equals("开")) {
                if (AppConfig.smo > Integer.valueOf(sharedPreferences.getString("smo_value", null))) {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
            if (sharedPreferences.getString("temp_state", null).equals("开")) {
                if (AppConfig.temp > Integer.valueOf(sharedPreferences.getString("temp_max", null))) {

                }
                if (AppConfig.temp > Integer.valueOf(sharedPreferences.getString("temp_low", null))) {

                }
            }
            handler.postDelayed(time, 1000);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    private void initView(View view) {
        sharedPreferences = getActivity().getSharedPreferences("rember", Context.MODE_PRIVATE);
        tv_co = (TextView) view.findViewById(R.id.tv_co);
        tv_gas = (TextView) view.findViewById(R.id.tv_co);
        tv_per = (TextView) view.findViewById(R.id.tv_co);
        tv_pm = (TextView) view.findViewById(R.id.tv_co);
        tv_press = (TextView) view.findViewById(R.id.tv_co);
        tv_smo = (TextView) view.findViewById(R.id.tv_co);
        cb_per = (CheckBox) view.findViewById(R.id.cb_per_state);
        lv_link_state = (ListView) view.findViewById(R.id.list_link_state);
    }
}
